package packege003.Myenum.bank;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;

import java.io.*;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/*
*
* 工厂类
*
* */
public class AccontFactory {
    private static DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    private  static DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static Accont createAccont(String name, String password, BigDecimal limit,UserStatus userStatus) throws Exception {
        Random random = new Random();
        String id;
        while (true){
            id = random.nextInt(10000000, 100000000)+"";
            Accont accontID = getAccontID(id);
            if (accontID == null){
                break;
            }
        }
        Accont accont = new Accont(id, name, password, BigDecimal.valueOf(0), limit, AccontStatus.OK, LocalDate.now(), LocalDateTime.now(), userStatus);
        PrintStream printStream = new PrintStream(new FileOutputStream("D:\\IDEA\\project001\\module001\\src\\packege003\\Myenum\\bank\\db", true));
        StringJoiner sj = getStringJoiner(accont);
        printStream.println(sj.toString());
        printStream.close();
        return accont;
    }

    private static StringJoiner getStringJoiner(Accont accont) {
        StringJoiner sj = new StringJoiner(",");
        sj.add(accont.getId());
        sj.add(accont.getName());
        sj.add(accont.getPassword());
        sj.add(accont.getBalance().toString());
        sj.add(accont.getLimit().toString());
        sj.add(accont.getStatus().toString());
        LocalDate createdtime = accont.getCreatedtime();//获取创建时间
        sj.add(createdtime.format(formatter1));
        sj.add(accont.getUpdatedtime().format(formatter2));
        sj.add(accont.getUserStatus().toString());
        return sj;
    }

    public static Accont getAccontID(String id) throws Exception {
        FileReader fileReader = new FileReader("D:\\IDEA\\project001\\module001\\src\\packege003\\Myenum\\bank\\db");
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String line;
        while ((line = bufferedReader.readLine())!=null){
            String[] arr = line.split(",");
            if (arr[0].equals(id)){
                return new Accont(arr[0],arr[1],arr[2],new BigDecimal(arr[3]),
                        new BigDecimal(arr[4]),AccontStatus.valueOf(arr[5]), LocalDate.parse(arr[6],formatter1),
                        LocalDateTime.parse(arr[7],formatter2),UserStatus.valueOf(arr[8]));
            }
        }
        return null;
    }

    public static void updateAccont(Accont loginUser) throws Exception {
        List<String> strings = getlist();
        File file = new File("D:\\IDEA\\project001\\module001\\src\\packege003\\Myenum\\bank\\db");
        strings.removeIf((S)->S.startsWith(loginUser.getId()));//这里我们把这个用户的信息删除
        strings.add(getStringJoiner(loginUser).toString());//再把新的信息添加进去
        FileUtils.writeLines(file, strings);//把这个集合写到这文件中
    }
    //把这个文件转换成集合，方便读取
    public static List <String>getlist() throws IOException {
        File file = new File("D:\\IDEA\\project001\\module001\\src\\packege003\\Myenum\\bank\\db");
        List<String> string = FileUtils.readLines(file, "utf-8");
        return string;
    }
}
